

#include "HeaderFiles.h"

uint8_t material_check_single[4]   = {0x55, 0xAA, 0x0D, 0x0C};
uint8_t material_check_start[4]    = {0x55, 0xAA, 0x0C, 0x0B};
uint8_t material_check_stop[4]     = {0x55, 0xAA, 0x0B, 0x0C};
uint8_t material_sensor_version[4] = {0x55, 0xAA, 0x89, 0x45};
uint8_t material_sensor_reset[4]   = {0x55, 0xAA, 0xFF, 0xFF};

static char sensor_rece_buf[64];
static char sensor_send_buf[64];

STR_RINGBUF material_rece_buf = {"UART4DMARecive", sensor_rece_buf, 64, 0, 0, 0, 0};
STR_RINGBUF material_send_buf = {"UART4DMASend"  , sensor_send_buf, 64, 0, 0, 0, 0};

STR_MATERIAL_CTRL material_sensor_ctrl;
STR_MATERIAL_ANALY material_analysis;


/*!
    \brief      none
    \param[in]  none
    \param[out] none
    \retval     none
*/

void material_cmd_send(void)
{
	if(material_sensor_ctrl.send_start == 0)
	{
		material_sensor_ctrl.cmd_buf = &material_check_single[0];
		
		material_sensor_ctrl.send_start = 1;
		material_sensor_ctrl.send_cnt = 0;
		material_sensor_ctrl.send_finish = 0;
		
		usart_interrupt_enable(UART4, USART_INT_TBE); 
	}		
}



/*!
    \brief      none
    \param[in]  none
    \param[out] none
    \retval     none
*/

void material_data_analysis(void)
{
	char receive_data = 0;
	int16_t receive_length = 0;
	
	receive_length = ringBufRead(&material_rece_buf, &receive_data, 1);
	
	if((receive_length != 0) || (material_analysis.FSM == M_ERRO))
	{
		switch(material_analysis.FSM)
		{
			case M_HEAD:
				if(receive_data == 0x5A)
				{
					material_analysis.buf.head = receive_data;
					
					material_analysis.FSM = M_DATA;
					
					material_analysis.rec_cnt = 1;
				}
			
				break;
			
			case M_DATA:
				
				material_analysis.buf.data[material_analysis.rec_cnt] = receive_data;
			
				material_analysis.rec_cnt++;
			
				if(material_analysis.rec_cnt == 19)
				{
					material_analysis.FSM = M_CHECK;
				}
			
				break;
			
			case M_CHECK:
				
				material_analysis.buf.data[material_analysis.rec_cnt] = receive_data;
			
				for(uint8_t i=0; i<19;i++)
				{
					material_analysis.data_sum += material_analysis.buf.data[i];
				}
				
				if((material_analysis.data_sum & 0x00FF) == material_analysis.buf.checsum)
				{
					material_analysis.floor_sort = material_analysis.buf.sort;
					
					material_analysis.FSM = M_HEAD;
					
					material_analysis.rec_msg_cnt++;
				}
				else
				{
					material_analysis.FSM = M_ERRO;
				}
				
				material_analysis.rec_cnt = 0;
				
				material_analysis.data_sum = 0;

				break;
			
			case M_ERRO:
			case M_OVERTIME:	
				material_analysis.floor_sort = 0xFF;
			
				material_analysis.FSM = M_HEAD;
			
				break;
						
			default:
				break;
		}
	}
}

/*!
    \brief      usart4 interrput service routine
    \param[in]  none
    \param[out] none
    \retval     none
*/

#if 1
void UART4_IRQHandler(void)
{
	// material sensor send cmd data -----------------------------------
	if(usart_interrupt_flag_get(UART4, USART_INT_FLAG_TBE) != RESET)
	{
		char send_data = 0;
		
		send_data = *(material_sensor_ctrl.cmd_buf + material_sensor_ctrl.send_cnt);
		
		usart_data_transmit(UART4, send_data);
		
		material_sensor_ctrl.send_cnt++;
		
		if(material_sensor_ctrl.send_cnt >= 4)
		{
			material_sensor_ctrl.send_start = 0;
			material_sensor_ctrl.send_finish = 1;
			usart_interrupt_disable(UART4, USART_INT_TBE); 
		}		
	}
	
	// materia sensor receive data -------------------------------------
	else if(usart_interrupt_flag_get(UART4, USART_INT_FLAG_RBNE) != RESET)
	{
		char receive_data = 0;
		
		usart_interrupt_flag_clear(UART4, USART_INT_FLAG_RBNE);
		
		receive_data = usart_data_receive(UART4);
		
		ringBufWrite(&material_rece_buf, &receive_data, 1);
	}
	
	else
	{
		#if 1
		volatile uint32_t tmp;

		tmp = USART_STAT0(UART4);
		tmp = USART_DATA(UART4);
		#endif
	}
}
#endif
